//
//  ShowCup.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-19.
//
//

#include "ShowCup.h"
#include "Resource.h"
#include "DataCenter.h"

ShowCup* ShowCup::createWithNameAndCap(std::string fileName,int cap)
{
    ShowCup *cup = new ShowCup();
    if (cup && cup->initWithNameAndCap(fileName, cap))
    {
        cup->autorelease();
        return cup;
    }
    else
    {
        CC_SAFE_DELETE(cup);
        return nullptr;
    }
    
}

bool ShowCup::initWithNameAndCap(std::string fileName,int cap)
{
    if(!ui::Button::init(fileName)){
        return false;
    }
    _moveing = false;
    m_capacity = cap;
    this -> setZoomScale(0);
    this -> setSwallowTouches(false);
    this -> addTouchEventListener(CC_CALLBACK_2(ShowCup::bottleClicked, this));
    
//    m_sprite = Sprite::create(fileName);
//    this -> addChild(m_sprite);
    m_capSpr = Sprite::create("cupNameBg.png");
    m_capSpr -> setPosition(Point(this -> getContentSize().width * 0.5f, -20));
    this -> addChild(m_capSpr);
    
    
    m_label = Label::createWithSystemFont("", "微软雅黑", 25);
    m_label -> setPosition(Point(m_capSpr -> getContentSize().width * 0.5f, m_capSpr -> getContentSize().height * 0.5f));
    m_capSpr -> addChild(m_label);
    
    if (m_capacity > 0) {
        m_label -> setString(Value(m_capacity).asString() + " ml");
    }else {
        m_label -> setString("新增");
    }
    
    cupOprBg = Sprite::create("cupOprBg.png");
    cupOprBg -> setPosition(Point(this -> getContentSize().width * 0.5f, this -> getContentSize().height * 0.85f));
    this -> addChild(cupOprBg);
    
    auto starBtn = ui::Button::create("del_cup_star@2x.png");
    starBtn -> setPosition(Point(cupOprBg -> getContentSize().width * 0.25f, cupOprBg -> getContentSize().height * 0.6f));
    cupOprBg -> addChild(starBtn);
    starBtn -> addTouchEventListener(CC_CALLBACK_2(ShowCup::starBtnPressed, this));
    starBtn -> setScale(1.1f);
    
    auto delBtn = ui::Button::create("del_cup_del@2x.png");
    delBtn -> setPosition(Point(cupOprBg -> getContentSize().width * 0.75f, cupOprBg -> getContentSize().height * 0.6f));
    cupOprBg -> addChild(delBtn);
    delBtn -> addTouchEventListener(CC_CALLBACK_2(ShowCup::delBtnPressed, this));
    delBtn -> setScale(1.1f);
    
    cupOprBg -> setVisible(false);
    return true;
}

void ShowCup::changeWithNameAndCap(std::string fileName,int cap)
{
    this -> loadTextureNormal(fileName);
    m_capacity = cap;
    if  (cap <= 0){
        m_label -> setString(StringUtils::format("新增"));
    }
    else {
        m_label -> setString(StringUtils::format("%d ml",cap));
    }
    cupOprBg -> setVisible(false);
}

ShowCup* ShowCup::createWithTypeAndCap(int type,int cap)
{
    ShowCup *cup = new ShowCup();
    if (cup && cup->initWithTypeAndCap(type, cap))
    {
        cup->autorelease();
        return cup;
    }
    else
    {
        CC_SAFE_DELETE(cup);
        return nullptr;
    }
}

bool ShowCup::initWithTypeAndCap(int type, int cap)
{
    if(!ui::Button::init(DataCenter::getInstance() -> getCupNameByType(type))){
        return false;
    }
    _moveing = false;
    _type = type;
    m_capacity = cap;
    this -> setZoomScale(0);
    this -> setSwallowTouches(false);
    this -> addTouchEventListener(CC_CALLBACK_2(ShowCup::bottleClicked, this));
    
    //    m_sprite = Sprite::create(fileName);
    //    this -> addChild(m_sprite);
    m_capSpr = Sprite::create("cupNameBg.png");
    m_capSpr -> setPosition(Point(this -> getContentSize().width * 0.5f, -20));
    this -> addChild(m_capSpr);
    
    
    m_label = Label::createWithSystemFont("", "微软雅黑", 25);
    m_label -> setPosition(Point(m_capSpr -> getContentSize().width * 0.5f, m_capSpr -> getContentSize().height * 0.5f));
    m_capSpr -> addChild(m_label);
    
    if (m_capacity > 0) {
        m_label -> setString(Value(m_capacity).asString() + " ml");
    }else {
        m_label -> setString("新增");
    }
    
    cupOprBg = Sprite::create("cupOprBg.png");
    cupOprBg -> setPosition(Point(this -> getContentSize().width * 0.5f, this -> getContentSize().height * 0.85f));
    this -> addChild(cupOprBg);
    
    auto starBtn = ui::Button::create("del_cup_star@2x.png");
    starBtn -> setPosition(Point(cupOprBg -> getContentSize().width * 0.25f, cupOprBg -> getContentSize().height * 0.6f));
    cupOprBg -> addChild(starBtn);
    starBtn -> addTouchEventListener(CC_CALLBACK_2(ShowCup::starBtnPressed, this));
    starBtn -> setScale(1.1f);
    
    auto delBtn = ui::Button::create("del_cup_del@2x.png");
    delBtn -> setPosition(Point(cupOprBg -> getContentSize().width * 0.75f, cupOprBg -> getContentSize().height * 0.6f));
    cupOprBg -> addChild(delBtn);
    delBtn -> addTouchEventListener(CC_CALLBACK_2(ShowCup::delBtnPressed, this));
    delBtn -> setScale(1.1f);
    
    cupOprBg -> setVisible(false);
    return true;
}

void ShowCup::changeWithTypeAndCap(int type,int cap)
{
    _type = type;
    this -> loadTextureNormal(DataCenter::getInstance() -> getCupNameByType(type));
    m_capacity = cap;
    if  (cap <= 0){
        m_label -> setString(StringUtils::format("新增"));
    }
    else {
        m_label -> setString(StringUtils::format("%d ml",cap));
    }
    cupOprBg -> setVisible(false);

}

void ShowCup::bottleClicked(Ref *pSender, ui::Widget::TouchEventType type)
{
    if  (type == ui::Widget::TouchEventType::BEGAN){
        _moveing = false;
        return;
    }
    if (type == ui::Widget::TouchEventType::MOVED) {
        _moveing = true;
        return;
    }
    if (type == ui::Widget::TouchEventType::ENDED && !_moveing) {
        PlayPositiveSound
        if (m_capacity > 0) {
            cupOprBg -> setScale(0.5);
            cupOprBg -> setVisible(true);
            ActionInterval *action = ScaleTo::create(0.3f, 1);
            cupOprBg -> runAction(EaseBackOut::create(action));
            NotificationCenter::getInstance() -> postNotification("hideTheOpr", this);
        }else {
            CCNotificationCenter::getInstance() -> postNotification("PopUpAddCupLayer");
        }
    }
}

void ShowCup::starBtnPressed(Ref *pSender,ui::Widget::TouchEventType type)
{
    if (type == ui::Widget::TouchEventType::ENDED && !_moveing) {
        __Dictionary *dict = __Dictionary::create();
        dict -> setObject(__Integer::create((int)_type), "Type");
        dict -> setObject(__Integer::create((int)m_capacity), "Capacity");
        CCNotificationCenter::getInstance() -> postNotification("ChangeTheDCup", dict);
    }
}

void ShowCup::delBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type == ui::Widget::TouchEventType::ENDED && !_moveing) {
        __Dictionary *dict = __Dictionary::create();
        dict -> setObject(__Integer::create((int)_cellIndex), "CellIndex");
        CCNotificationCenter::getInstance() -> postNotification("DeleteCup", dict);
    }
}

void ShowCup::onEnter()
{
    ui::Button::onEnter();
    NotificationCenter::getInstance() -> addObserver(this, callfuncO_selector(ShowCup::hideTheOpr), "hideTheOpr", NULL);
}

void ShowCup::onExit()
{
    ui::Button::onExit();
    NotificationCenter::getInstance() -> removeAllObservers(this);
}

void ShowCup::hideTheOpr(Ref *pSender)
{
    auto cup = dynamic_cast<ShowCup *>(pSender);
    if (cup != this && this -> cupOprBg -> isVisible()) {
        this -> cupOprBg -> setVisible(false);
    }
}